#!/bin/bash
#finding all pdf file under current directory
#@author zaky.rahim@gmail.com
find . | grep -e "\.pdf$" | while read -r fname
do
        #renaming file with whitespace. replace whitespace with underscore
        #all character changed to lower case one
        rname=$fname
        renamed=$(echo $fname | sed -e "s/ /_/g" | tr '[A-Z]' '[a-z]')
        #check if there is file with space or Upper case name
        c_space=$(echo "$rname" | grep -e '.* .*')
        lc_space=$(echo "$rname" | grep -e '.*[A-Z].*')
        
        count=$(expr ${#c_space} + ${#lc_space})
        echo $count
        #rename if file found
        if [ $count -gt 0 ]
        then 
                mv "$rname" "$renamed"
        fi

        #convert pdf to text file, then push it to postgres database
                echo $(echo "insert into files(file_name, file_content) \
                        values (' $renamed','" $( pdftotext $renamed - | \
                        sed -e "s/'/''/g") "');") >> sql_statement.txt
        psql -U postgres -h yourhost.gov -d search_engine -f sql_statement.txt
        rm sql_statement.txt #chop chop , clean your mess.
done
